package com.lcc.message_wall.controller;

import com.lcc.message_wall.model.MessageInfo;
import com.lcc.message_wall.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("/message")
public class MessageController {
//    List<MessageInfo> messageInfos = new ArrayList<>();//先存放再内存中
    //下面修改到MySQL数据库中
    @Autowired
    private MessageService messageService;
    //发布留言
    @RequestMapping("/publish")
    public Boolean publish(MessageInfo messageInfo){
        //进行数据的校验---->这是Controller的工作
        if (!StringUtils.hasLength(messageInfo.getFrom()) || !StringUtils.hasLength(messageInfo.getTo()) || !StringUtils.hasLength(messageInfo.getMessage())){
            return false;
        }
        //添加留言---->业务逻辑:添加到service里面去,然后直接调用
        messageService.addMessage(messageInfo);
//        messageInfos.add(messageInfo);
        return true;
    }

    //获取所有的留言
    @RequestMapping("/getMessageInfo")
    public List<MessageInfo> getMessageInfo(){
        return messageService.getMessageInfo();
    }
}
